package com.applcn.file.service;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.applcn.core.base.Result;
import com.applcn.file.dto.GetCertificationDTO;
import com.applcn.file.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * com.applcn.file.service
 * Description:
 *
 * @author: 大妖怪
 * @date: 2020-03-30 00:07
 */
@Service
@RequiredArgsConstructor
public class FileService {

    private final FileUtil fileUtil;

    /**
     * 获取文件上传预签名url
     *
     * @return
     */
    public Result getCertification(String prefix, String suffix) {
        String fileName = "";
        if (StrUtil.isNotBlank(prefix)) {
            fileName += prefix + "/";
        }
        fileName += IdUtil.simpleUUID();
        if (StrUtil.isNotBlank(suffix)) {
            fileName += "." + suffix;
        }
        String fileUrl = fileUtil.getFileUrl(fileName);
        String certificationUrl = fileUtil.getCertification(fileName);
        GetCertificationDTO dto = new GetCertificationDTO();
        dto.setCertificationUrl(certificationUrl);
        dto.setUrl(fileUrl);
        return Result.ok().putData(dto);
    }
}